package com.cx.bank.view;

import com.cx.bank.dao.BankDaoInterface;
import com.cx.bank.factory.UserDaoFactory;
import com.cx.bank.handler.MyInVocationHandler;
import com.cx.bank.manager.IManager;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.pojo.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author jxau_scf
 * @date 2021/7/19 - 17:08
 */
public class BankRegisterView extends JFrame {
    private JButton register;
    private JLabel idLabel, codeLabel, moneyLabel, bank, userTypeLabel;
    public JTextField idText, montyText,userTypeText;
    private JPasswordField codeText;

    public BankRegisterView(){
        building();
        addlisteners();
    }



    private void building() {
        setLayout(null);
        //标签
        bank = new JLabel("bank");
        idLabel = new JLabel("账号");
        codeLabel = new JLabel("密码");
        moneyLabel = new JLabel("开户");
        userTypeLabel = new JLabel("类型");
        //按钮
        register = new JButton("注册");

        idText = new JTextField(20);
        montyText = new JTextField(20);
        codeText = new JPasswordField(20);
        userTypeText = new JTextField(20);

        bank.setBounds(300,20,120,50);
        Font font = new Font("圆体", Font.BOLD, 50);
        Font font1 = new Font("圆体", Font.BOLD, 30);
        bank.setFont(font);
        register.setBounds(150, 500, 120, 25);
        idLabel.setBounds(150, 200, 120, 25);
        idLabel.setFont(font1);
        codeLabel.setBounds(150, 250, 120, 25);
        codeLabel.setFont(font1);
        moneyLabel.setBounds(150, 300, 120, 25);
        moneyLabel.setFont(font1);
        userTypeLabel.setBounds(150, 350, 120, 25);
        userTypeLabel.setFont(font1);
        idText.setBounds(250, 200, 200, 30);
        codeText.setBounds(250, 250, 200, 30);
        montyText.setBounds(250, 300, 200, 30);
        userTypeText.setBounds(250, 350, 200, 30);

        add(bank);
        add(idLabel);
        add(idText);
        add(codeLabel);
        add(codeText);
        add(moneyLabel);
        add(montyText);
        add(userTypeLabel);
        add(userTypeText);
        add(register);

        setBounds(600,600,600,600);
        setLocation((2000-getWidth())/2,(1000-getHeight())/2);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    private void addlisteners() {
        register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                User user = null;

                String userName = idText.getText();
                String password = codeText.getText();
                String money = montyText.getText();
                String userType = userTypeText.getText();
                user = new User(userName,password,1, userType, Double.parseDouble(money));

                UserDaoFactory userDaoFactory = null;
                try {
                    userDaoFactory = UserDaoFactory.getInstance();
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IOException ex) {
                    ex.printStackTrace();
                }
                ManagerImpl manager = ManagerImpl.getInstance();
                BankDaoInterface bankDao = userDaoFactory.getBankDao();
                manager.setBankDao(bankDao);

                MyInVocationHandler handler = new MyInVocationHandler();
                handler.setTarget(manager);
                handler.setUserName(userName);
                IManager proxy = (IManager)handler.getProxy();


                try {
                    boolean registerStatus = proxy.register(user);
                    if (registerStatus){
                        //注册成功，关闭当前页面
                        BankRegisterView.this.dispose();
                        //打开BankView
                        new BankView().setVisible(true);
                    }else {
                        JOptionPane.showMessageDialog(register, "注册失败", "标题", JOptionPane.WARNING_MESSAGE);
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }

            }
        });
    }

    public static void main(String[] args) {
        BankRegisterView bankRegisterView = new BankRegisterView();
        bankRegisterView.show();
    }
}
